package de.ipk_gatersleben.bit.bi.edal.publication.attribute.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.CellEditor;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableModel;
import de.ipk_gatersleben.bit.bi.edal.primary_data.metadata.LegalPerson;
import de.ipk_gatersleben.bit.bi.edal.publication.AttributeLabel;
import de.ipk_gatersleben.bit.bi.edal.publication.AttributeTablePanel;
import de.ipk_gatersleben.bit.bi.edal.publication.Utils;
import de.ipk_gatersleben.bit.bi.edal.publication.PropertyLoader;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationFrame;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationMainPanel;
import de.ipk_gatersleben.bit.bi.edal.publication.PublicationModul;

public class PublisherPanel extends JPanel implements ActionListener {

	private static final Dimension SCROLLPANE_SIZE = new Dimension(
			PropertyLoader.MINIMUM_DIM_PUBLICATION_FRAME.width,
			PropertyLoader.PUBLISHER_PANEL_HEIGHT);

	private static final long serialVersionUID = 8109825692298261311L;

	private static final AttributeLabel PUBLISHER_LABEL = new AttributeLabel(
			PropertyLoader.props.getProperty("PUBLISHER_LABEL"),
			PropertyLoader.props.getProperty("PUBLISHER_TOOLTIP"));;

	private static final JButton OKAY_BUTTON = new JButton("OK");

	private DefaultTableModel model;
	private JTable table;

	private static final String[] DEFAULT_PUBLISHER = new String[] {
			"Leibniz Institute of Plant Genetics and Crop Plant Research (IPK)",
			"Gatersleben, Corrensstraße 3", "06466", "Germany" };

	private static final String[] COL_NAMES = { "Name", "Address", "Zip",
			"Country" };

	public PublisherPanel() {

		GridBagLayout grid = new GridBagLayout();

		JPanel mainPanel = new JPanel(grid);

		mainPanel.setBackground(PropertyLoader.MAIN_BACKGROUND_COLOR);

		model = new MyTableModel();
		table = new JTable(model);// {

		table.setFillsViewportHeight(true);
		table.setFont(PropertyLoader.DEFAULT_FONT);
		table.setForeground(PropertyLoader.MAIN_FONT_COLOR);
		table.getTableHeader().setFont(PropertyLoader.DEFAULT_FONT);
		table.getTableHeader().setForeground(PropertyLoader.MAIN_FONT_COLOR);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getTableHeader().setBorder(
				BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));

		table.getColumnModel().getColumn(0)
				.setPreferredWidth((SCROLLPANE_SIZE.width / 100) * 50);
		table.getColumnModel().getColumn(1)
				.setPreferredWidth((SCROLLPANE_SIZE.width / 100) * 30);
		table.getColumnModel().getColumn(2)
				.setPreferredWidth((SCROLLPANE_SIZE.width / 100) * 5);
		table.getColumnModel().getColumn(3)
				.setPreferredWidth((SCROLLPANE_SIZE.width / 100) * 10);

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.setBackground(PropertyLoader.MAIN_BACKGROUND_COLOR);

		buttonPanel.add(OKAY_BUTTON);

		OKAY_BUTTON.addActionListener(this);

		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(SCROLLPANE_SIZE);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());

		Utils.addComponent(mainPanel, grid, scrollPane, 0, 0, 1, 1, 1, 0.95, 1,
				1);
		Utils.addComponent(mainPanel, grid, buttonPanel, 0, 1, 1, 1, 1, 0.05,
				1, 1);

		JPanel attributePanel = new JPanel(new BorderLayout());

		PublisherPanel.PUBLISHER_LABEL
				.setForeground(PropertyLoader.LABEL_COLOR);

		attributePanel.add(PublisherPanel.PUBLISHER_LABEL, BorderLayout.NORTH);
		attributePanel.setBackground(PropertyLoader.MAIN_BACKGROUND_COLOR);
		attributePanel.setPreferredSize(new Dimension(40,
				PropertyLoader.PUBLISHER_PANEL_HEIGHT));

		this.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));

		GridBagLayout gridbaglayout = new GridBagLayout();

		this.setLayout(gridbaglayout);
		this.setBackground(PropertyLoader.MAIN_BACKGROUND_COLOR);

		Utils.addComponent(this, gridbaglayout, attributePanel, 0, 0, 1, 1,
				0.02, 1, 50, 10);
		Utils.addComponent(this, gridbaglayout, mainPanel, 1, 0, 1, 1, 0.98, 1,
				50, 10);

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {

		if (actionEvent.getSource().equals(OKAY_BUTTON)) {

			CellEditor cellEditor = table.getCellEditor();
			if (cellEditor != null) {
				if (cellEditor.getCellEditorValue() == null) {
					cellEditor.stopCellEditing();
				}
			}

			/* clear(unselect) selected rows */
			this.table.clearSelection();

			PublicationMainPanel.publisherPanel = this;

			PublicationMainPanel.publisherField.setText(getTableContent());

			PublicationMainPanel.releaseAllBlockedFields();

			PublicationMainPanel.getMainPanel().remove(
					PublicationMainPanel.PUBLISHER_PANEL_COMPONENT_NUMBER);

			PropertyLoader.PUBLISHER_LABEL
					.setForeground(PropertyLoader.LABEL_COLOR);

			PublicationMainPanel.getMainPanel().add(
					new AttributeTablePanel(PropertyLoader.PUBLISHER_LABEL,
							PublicationMainPanel.publisherField,
							(PropertyLoader.PUBLISHER_PANEL_HEIGHT)),
					PublicationMainPanel.PUBLISHER_PANEL_COMPONENT_NUMBER);

			PublicationFrame.updateUI();
			PublicationModul.getFrame().requestFocusInWindow();

		}

	}

	private class MyTableModel extends DefaultTableModel {

		private static final long serialVersionUID = 8557354261856948625L;

		private Object[][] data = { DEFAULT_PUBLISHER };

		public MyTableModel() {
			setDataVector(data, COL_NAMES);
		}
	}

	private String getTableContent() {

		String seperator = new String(", ");

		StringBuffer buffer = new StringBuffer("");

		if (!Utils.checkIfStringIsEmpty(this.model.getValueAt(0, 0).toString())) {

			buffer.append(this.model.getValueAt(0, 0).toString());
			buffer.append(seperator);
		}
		if (!Utils.checkIfStringIsEmpty(this.model.getValueAt(0, 1).toString())) {

			buffer.append(this.model.getValueAt(0, 1).toString());
			buffer.append(seperator);
		}
		if (!Utils.checkIfStringIsEmpty(this.model.getValueAt(0, 2).toString())) {

			buffer.append(this.model.getValueAt(0, 2).toString());
			buffer.append(seperator);
		}

		if (!Utils.checkIfStringIsEmpty(this.model.getValueAt(0, 3).toString())) {

			buffer.append(this.model.getValueAt(0, 3).toString());
		}

		if (Utils.checkIfStringIsEmpty(buffer.toString())) {

			Object[][] data = { DEFAULT_PUBLISHER };
			this.model.setDataVector(data, COL_NAMES);
			return PublicationMainPanel.DEFAULT_PUBLISHER_STRING;
		} else {
			return buffer.toString();
		}

	}

	public LegalPerson getPublisher() {

		String legalName = "unkown";
		String addressLine = "unkown";
		String zip = "unkown";
		String country = "unkown";

		if (!Utils.checkIfStringIsEmpty(this.model.getValueAt(0, 0).toString())) {
			legalName = this.model.getValueAt(0, 0).toString();
		}
		if (!Utils.checkIfStringIsEmpty(this.model.getValueAt(0, 1).toString())) {
			addressLine = this.model.getValueAt(0, 1).toString();
		}
		if (!Utils.checkIfStringIsEmpty(this.model.getValueAt(0, 2).toString())) {
			zip = this.model.getValueAt(0, 2).toString();
		}
		if (!Utils.checkIfStringIsEmpty(this.model.getValueAt(0, 3).toString())) {
			country = this.model.getValueAt(0, 3).toString();
		}

		LegalPerson legalPerson = new LegalPerson(legalName, addressLine, zip,
				country);

		return legalPerson;
	}
}
